﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CFM.DAL;
using CFM.DTO;

namespace CFM.API
{
    public class CategoriesAPI
    {
        private DataHelper _helper = null;
        public CategoriesAPI()
        {
            _helper = new DataHelper();
        }

        public List<Categories> GetAll()
        {
            string sql = "select * from Categories";        
            SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
            return _helper.MapDataReaderToObject<Categories>(dr);
        }
        public List<Categories> GetAllForSale()
        {
            string sql = "select * from Categories where Categoryid != 'nguyenlieu' ";
            SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
            return _helper.MapDataReaderToObject<Categories>(dr);
        }

        public bool Create(Categories cate)
        {
            string sql = "insert into Categories(CategoryId,CategoryName) values(@CategoryId,@CategoryName)";
            SqlParameter[] prs = 
            {
                new SqlParameter("@CategoryId",cate.CategoryId),
                new SqlParameter("@CategoryName",cate.CategoryName)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Update(Categories cate)
        {
            string sql = "update Categories set CategoryName = @CategoryName where CategoryId = @CategoryId";
            SqlParameter[] prs = 
            {
                new SqlParameter("@CategoryId",cate.CategoryId),
                new SqlParameter("@CategoryName",cate.CategoryName)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Delete(string cateId)
        {
            string sql = "delete Categories where CategoryId = @CategoryId";
            SqlParameter[] prs = 
            {
                new SqlParameter("@CategoryId",cateId)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }
       
    }
}
